#include"../../include/hashdup.h"

Hashdup::Hashdup()
: simhasher("../include/simhash/dict/jieba.dict.utf8", "../include/simhash/dict/hmm_model.utf8", "../include/simhash/dict/idf.utf8", "../include/simhash/dict/stop_words.utf8")
 /* :simhasher(Configuration::getInstance()->getConfigMap().find("DICT_PATH")->second */
 /* , Configuration::getInstance()->getConfigMap().find("HMM_PATH")->second */
 /* , Configuration::getInstance()->getConfigMap().find("IDF_PATH")->second */
 /* , Configuration::getInstance()->getConfigMap().find("STOP_WORD_PATH")->second) */
{

}

Hashdup::AutoRelease::AutoRelease(){
    /* cout<<"AutoRelease()"<<endl; */
}
Hashdup::AutoRelease::~AutoRelease(){
    /* cout<<"~AutoRelease()"<<endl; */
    if(_pHashdup){
        delete _pHashdup;
        _pHashdup = nullptr;
    }
}

Hashdup*Hashdup::getpHashdup(){
    if(_pHashdup == nullptr){
        _pHashdup = new Hashdup();
    }
    return _pHashdup;
}

//计算海明距离
int Hashdup::calculateHammingDistance(uint64_t hash1, uint64_t hash2){
    std::bitset<64> bits1(hash1);
    std::bitset<64> bits2(hash2);
    int distance = (bits1 ^ bits2).count();
    /* cout<<"distance: "<<distance<<endl; */
    return distance;
}

uint64_t Hashdup::getHash(const string &s1){
    /* cout << "执行了getHash" << endl; */
    size_t topN = 100;
    uint64_t u64 = 0;
    // uint64_t u642 = 0; // 不再需要这行
    vector<pair<string, double>> res;
    simhasher.extract(s1, res, topN);
    simhasher.make(s1, topN, u64);
    return u64; // 返回哈希值
}

//比较哈希值
bool Hashdup::simHash1(size_t u64, size_t u642){
    /* cout << "比较 Simhash 值的相似性" << endl; */
    if (calculateHammingDistance(u64, u642) < 12) {
        /* cout << "是相似的" << endl; */
        return true;
    } else {
        /* cout << "不相似" << endl; */
        return false;
    }
}
Hashdup *Hashdup::_pHashdup = getpHashdup();
Hashdup::AutoRelease Hashdup::_ar;
